package org.hs.im.forensicj.view.imageviewer;

import java.awt.image.RenderedImage;

import javax.swing.JScrollPane;

import org.hs.im.forensicj.Resources;
import org.hs.im.forensicj.view.ImageMultipleCDockableLayout;
import org.hs.im.forensicj.view.ImageViewer;
import org.hs.im.forensicj.view.ImageViewerListener;
import org.hs.im.forensicj.view.imageviewer.action.CopyButton;
import org.hs.im.forensicj.view.imageviewer.action.CropToSelectionButton;
import org.hs.im.forensicj.view.imageviewer.action.CutButton;
import org.hs.im.forensicj.view.imageviewer.action.FillWindowButton;
import org.hs.im.forensicj.view.imageviewer.action.FitToWindowButton;
import org.hs.im.forensicj.view.imageviewer.action.FlipHorizontalButton;
import org.hs.im.forensicj.view.imageviewer.action.FlipVerticalButton;
import org.hs.im.forensicj.view.imageviewer.action.MouseModeNormalButton;
import org.hs.im.forensicj.view.imageviewer.action.MouseModePanButton;
import org.hs.im.forensicj.view.imageviewer.action.MouseModeSelectButton;
import org.hs.im.forensicj.view.imageviewer.action.MouseModeZoomInButton;
import org.hs.im.forensicj.view.imageviewer.action.MouseModeZoomOutButton;
import org.hs.im.forensicj.view.imageviewer.action.MouseModeZoomToRegionButton;
import org.hs.im.forensicj.view.imageviewer.action.PasteButton;
import org.hs.im.forensicj.view.imageviewer.action.RedoButton;
import org.hs.im.forensicj.view.imageviewer.action.RotateLeftButton;
import org.hs.im.forensicj.view.imageviewer.action.RotateRightButton;
import org.hs.im.forensicj.view.imageviewer.action.UndoButton;
import org.hs.im.forensicj.view.imageviewer.action.ZoomActualResolutionButton;

import bibliothek.gui.dock.common.DefaultMultipleCDockable;
import bibliothek.gui.dock.common.MultipleCDockableFactory;
import bibliothek.gui.dock.common.action.CRadioButton;
import bibliothek.gui.dock.common.action.CRadioGroup;

public class ImageDockable extends DefaultMultipleCDockable {

	public static final MultipleCDockableFactory<ImageDockable, ImageMultipleCDockableLayout> FACTORY = new ImageMultipleCDockableFactory();
	private RenderedImage image;
	private ImageViewer viewer;
	private final String name;
	
	
	public ImageDockable(String name, RenderedImage image) {
		super(FACTORY);
		this.name = name;
		
        setCloseable( true );
        setMinimizable( true );
        setMaximizable( true );
        setExternalizable( false );
        setRemoveOnClose( true );

        setTitleIcon( Resources.getIcon( "dockable.image" ) );

		this.image = image;
		viewer = new DefaultImageViewer(this.image);
		viewer.addImageViewerListener( new ImageViewerListener(){
			
			@Override
			public void zoomChanged(double newZoomLevel){
				updateTitle();
			}
			
		});
		
		updateTitle();

		JScrollPane scroller = new JScrollPane(viewer.getComponent());
		
		getContentPane().add(scroller);
		

		
		CRadioGroup mouseModeGroup = new CRadioGroup();

        CRadioButton normalMouseMode = new MouseModeNormalButton( viewer );
        CRadioButton panMouseMode = new MouseModePanButton( viewer );
        CRadioButton selectMouseMode = new MouseModeSelectButton( viewer );
        CRadioButton zoomInMouseMode = new MouseModeZoomInButton( viewer );
        CRadioButton zoomOutMouseMode = new MouseModeZoomOutButton( viewer );
        CRadioButton zoomToRegionMouseMode = new MouseModeZoomToRegionButton( viewer );
        
        mouseModeGroup.add(normalMouseMode);
        mouseModeGroup.add(panMouseMode);
        mouseModeGroup.add(selectMouseMode);
        mouseModeGroup.add(zoomInMouseMode);
        mouseModeGroup.add(zoomOutMouseMode);
        mouseModeGroup.add(zoomToRegionMouseMode);
        
        normalMouseMode.setSelected(true);
        
        addAction( normalMouseMode );
        addAction( panMouseMode );
        addAction( selectMouseMode );
        addAction( zoomInMouseMode );
        addAction( zoomOutMouseMode );
        addAction( zoomToRegionMouseMode );
        addSeparator();

        addAction( new ZoomActualResolutionButton( viewer ) );
        addAction( new FitToWindowButton( viewer ) );
        addAction( new FillWindowButton( viewer ) );
        addSeparator();
        addAction( new RotateLeftButton( viewer ) );
        addAction( new RotateRightButton( viewer ) );
        addSeparator();
        addAction( new FlipHorizontalButton( viewer ) );
        addAction( new FlipVerticalButton( viewer ) );
        addSeparator();
        addAction( new CropToSelectionButton( viewer ) );
        addSeparator();
        addAction( new CopyButton( viewer ) );
        addAction( new CutButton( viewer ) );
        addAction( new PasteButton( viewer ) );
        addSeparator();
        addAction( new UndoButton( viewer ) );
        addAction( new RedoButton( viewer ) );
        
        
        
	}


	private void updateTitle() {
		setTitleText(name + " [" + (float)(100*viewer.getZoomLevel()) + "%]");
	}


	public ImageViewer getImageViewer() {
		return viewer;
	}


}
